import { Component } from "react";
import PropTypes from "prop-types";
import Votemain from "./Votemain";
import Votefooter from "./Votefooter";
import sub from "./sub";

export default class Vote extends Component{
    static propTypes={//校验title格式
        title:PropTypes.string.isRequired
    }
    static defaultProps={
        title:"vue好学吗？"
    }
    state={
        total:0
    }
    render(){
        let {title}=this.props;
        return <div>
            <h1>{title}---总人数：{this.state.total}</h1>
            <Votemain></Votemain>
            <Votefooter></Votefooter>
        </div>
    }
    addTotal=()=>{
        this.setState({
            total:this.state.total+1
        })
    }
    componentDidMount(){
        sub.on("addTotal",this.addTotal)
    }
    componentWillUnmount(){
        sub.off("addTotal",this.addTotal)
    }
}